/*
 * Copyright: Tianjin Berheley Tech Co,.ltd, China
 * biPlatForm
 * com.berheley.bi.beanProcess -> BeanPropertyListAppender.java
 * Created on 2009-10-21-下午01:25:18
 */
package com.webframe.beanProcess;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.webframe.exception.JSException;



/**
 * 类功能描述：
 *
 * @author      <a href="mailto:tozxwu@hotmail.com">wuzhenxue </a>
 * Create:      2009-10-21 下午01:25:18
 * Description: 
 */
public class BeanPropertyListAppender extends AbstractBeanPropertyProcessor {
	
	private static final Log log = LogFactory.getLog(BeanPropertyListAppender.class);

	private Object appended;
	private int index = -1;

	@SuppressWarnings({
				"rawtypes", "unchecked"})
	protected Object getProcessedPropertyValue(Object originalValue) {
		List newValue;
		if (originalValue == null) {
			newValue = new ArrayList();
		} else {
			if (!(originalValue instanceof List)) {
				throw new JSException("jsexception.property.not.a.list", new Object[] {getPropertyName(), getBeanName()});
			}
			newValue = (List) originalValue;
		}
		if (appended instanceof List) {
			List appendedList = (List) appended;
			if (index >= 0) {
				newValue.addAll(index, appendedList);
				
				if (log.isInfoEnabled()) {
					log.info("Inserting " + appendedList.size() + " entries at index " + index 
							+ " into " + getBeanName() + "." + getPropertyName());
				}
			} else {
				newValue.addAll(appendedList);
				
				if (log.isInfoEnabled()) {
					log.info("Adding " + appendedList.size() + " entries to " 
							+ getBeanName() + "." + getPropertyName());
				}
			}
		} else {
			if (index >= 0) {
				newValue.add(index, appended);
				
				if (log.isInfoEnabled()) {
					log.info("Inserting 1 entry at index " + index 
							+ " into " + getBeanName() + "." + getPropertyName());
				}
			} else {
				newValue.add(appended);
				
				if (log.isInfoEnabled()) {
					log.info("Adding 1 entry to " 
							+ getBeanName() + "." + getPropertyName());
				}
			}
		}
		return newValue;
	}

	public Object getAppended() {
		return appended;
	}

	public void setAppended(Object appended) {
		this.appended = appended;
	}
	
	public int getIndex() {
		return index;
	}

	public void setIndex(int index) {
		this.index = index;
	}

}

